package com.zhangli.communication.email;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;

/**
 * @author wuhn
 * @创建时间 2015-12-18
 * @功能 javamail 邮件检查和提取
 **/
public class Receive {
    /**
     * @param SendEmailModel sendMailModel
     * @return Properties properties
     * @功能 设置连接pop3邮件的属性    邮件接收服务器
     **/
    public static Properties setPop3Properties(Address sendMailModel) {
        Properties properties = new Properties();
        properties.put("mail.pop3.host", sendMailModel.getHost());
        properties.put("mail.pop3.port", sendMailModel.getPort());
        properties.put("mail.pop3.starttls.enable", "true");

        return properties;
    }


    /**
     * @param SendEmailModel sendMailModel
     *                       Properties properties
     * @return Store store
     * @throws MessagingException
     * @功能 设置创建pop3 store并连接pop服务器
     **/
    public static Store getStore(Address sendMailModel, Properties properties) throws MessagingException {
        //获取session
        Session emailSession = Session.getDefaultInstance(properties);
        emailSession.setDebug(false);
        //创建pop3 store
        Store store = emailSession.getStore("pop3s");
        //连接pop服务器
        store.connect(sendMailModel.getHost(), sendMailModel.getUsername(), sendMailModel.getPassword());
        return store;
    }


    /**
     * @param SendEmailModel sendMailModel
     * @return void
     * @功能 邮件检查和提取
     **/
    public static void receiveEmail(Address sendMailModel) {
        Properties properties = setPop3Properties(sendMailModel);

        try {
            Store store = getStore(sendMailModel, properties);
            //创建一个文件夹并打开它
            Folder emailFolder = store.getFolder("INBOX");
            emailFolder.open(Folder.READ_ONLY);
            //从folder中获取这些邮件信息并打印出来
            Message[] messages = emailFolder.getMessages();

            System.out.println("邮件总数：" + messages.length);

            for (int i = 0, n = messages.length; i < n; i++) {
                Message message = messages[i];
                System.out.println("---------------------------------");
                System.out.println("Email Number " + (i + 1));
                System.out.println("Subject: " + message.getSubject());
                System.out.println("From: " + message.getFrom()[0]);
                System.out.println("Text: " + message.getContent().toString());
            }
            //关闭store和Folder
            emailFolder.close(false);
            store.close();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String receiveLastEmailForTitle(Address sendMailModel) {
        Properties properties = setPop3Properties(sendMailModel);
        try {
            Store store = getStore(sendMailModel, properties);
            //创建一个文件夹并打开它
            Folder emailFolder = store.getFolder("INBOX");
            emailFolder.open(Folder.READ_ONLY);
            //从folder中获取这些邮件信息并打印出来
            Message[] messages = emailFolder.getMessages();
            String content = "";
            if (messages.length > 0)
                content = messages[messages.length - 1].getSubject();
            //关闭store和Folder
            emailFolder.close(false);
            store.close();
            return content;
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return "";
    }
}
